$(function() {

    $.get("/static/html/comm/nvg.html", function(data) {
        $(".container").prepend(data);
        // 初始化
        init();
    });

    $.get("/static/html/comm/login.html", function(data) {
        $("body").append(data);
    });

    $.get("/static/html/comm/register.html", function(data) {
        $("body").append(data);
    });

    function init() {
        $.get('/isLogin', function(data) {
            if (data && data.retCode == 0 && data.retVal) {
                $('#no-login').hide();
                sessionStorage.setItem("account", data.retMsg);
                $('#loginUser').empty().html(data.retMsg + '<span class="caret"></span>');
                $('#isLogin').show();
            } else {
                $('#isLogin').hide();
            }
        });
    }

    function loginCheck() {
        var account = $("#account").val();
        var password = $("#password").val();
        if (null == account || account == 'undefined' || account == '' || null == password || password == 'undefined' || password == '') {
            return false;
        } else {
            return true;
        }
    }

    $('body').on('click', '#loginBtn', function(event) {
        login();
    });

    $('body').on('focus change', '#login-modal input', function() {
        $('#tip').hide();
    });

    $('body').on('keyup', '#login-modal input', function(event) {
        if (event.keyCode == 13) {
            login();
        }
    });

    $('body').on('click', '#registerBtn', function(event) {
        if (registerCheck()) {
            var user = {
                "name": $("#regName").val(),
                "account": $("#regAccount").val(),
                "password": $("#regPwd").val()
            };
            $.post("/user/register", user, function(data) {
                if (0 == data.retCode) {
                    alert("注册成功。");
                    $('#register-modal').modal('hide');
                } else {
                    alert("注册失败：" + data.retMsg);
                }
            });
        }
    });

    function registerCheck() {
        var pwd = $("#regPwd").val();
        if (pwd)
            return true;
    }

    function login() {
        var self = $(this);
        var account = $("#account").val();
        if (loginCheck()) {
            $.post('/loginAjax', {
                'account': account,
                'password': $("#password").val()
            }, function(data, textStatus, xhr) {
                if (data && data.retCode == 0) {
                    // 缓存帐号
                    sessionStorage.setItem("account", account);
                    $('#no-login').hide();
                    $('#loginUser').prepend(sessionStorage.getItem("account"));
                    $('#isLogin').show();
                    $('#login-modal').modal('hide');
                    var url = window.location.href;
                    var urlArr = url.split('//');
                    if (null != urlArr && urlArr.length >= 2) {
                        var path = urlArr[1].split('/');
                        if (null == path || path.length <= 1) {
                            window.location.reload();
                            return;
                        }
                        if ('' == path[1] || null == path[1]) {
                            window.location.href = '/static/html/projects/manager.html';
                            return;
                        }
                    }
                    window.location.reload();
                } else {
                    $('#tip').show();
                    $('#tip-content').text(data.retMsg);
                }
            });
        }
    }

    /*$('body').on('click', '#qq_login_btn', function(event) {
        QC.Login({ //按默认样式插入QQ登录按钮
                    btnId: "qq_login_btn" 
        });
    });*/


});